package com.qf.cloud2203.coupon.service.impl;

import com.qf.cloud2203.coupon.entity.Coupon;
import com.qf.cloud2203.coupon.mapper.CouponMapper;
import com.qf.cloud2203.coupon.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.cloud2203.docoupon.utlis.RandomUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苇名一心
 * @since 2023-03-10
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired
    RedisTemplate redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void getCoupons() {

        //创建集合准备落库
       List<Coupon> listCoupons = new ArrayList<Coupon>();



        for (int i = 0; i < 100; i++){

            //创建优惠卷对象
            Coupon coupon = new Coupon();
            /**
             * 调用工具类获取优惠卷的信息
             */
            String id = RandomUUID.getId();
            double money = RandomUUID.getMoney();
            String scope = RandomUUID.getScope(money);

            /**
             * 封装对象
             */
            coupon.setId(id);
            coupon.setMoney(money);
            coupon.setScope(scope);
            Date date = new Date();
            Date newDate = new Date();
            newDate.setTime(date.getTime()+1000*60*60*24*10);
            coupon.setCreatetime(date);
            coupon.setTimelimit(newDate);

            //添加集合
            listCoupons.add(coupon);

        }

        //落库
        saveBatch(listCoupons);

        //写入redis中
        List<Coupon> list = list();
        for (Coupon coupon : list) {
            redisTemplate.opsForList().rightPush("weimingyixinlist",coupon);
        }
    }
}
